package org.gfix.binding;

import java.io.StringReader;

import org.gfix.parser.FixParser;
import org.gfix.parser.MessageSource;
import org.junit.Assert;
import org.junit.Test;



public class FIXToClassBindingTest {

	@Test
	public void checkClasses() throws Exception
	{
		FixParser fp = new FixParser();
		FixBinder fb = new FixBinder(fp);
		fb.register(FixMessage1.class);
		fb.register(FixMessage2.class);
		fb.register(FixMessage3.class);
		fb.read(new MessageSource(new StringReader("8=FIX.4.1\00135=D\00110=10")));
		Assert.assertNotNull(fb.getCurrentMessageObject());
		Assert.assertTrue(fb.getCurrentMessageObject() instanceof FixMessage1);		
	}
	
	@Test
	public void checkClassesMutliCalls() throws Exception
	{
		FixParser fp = new FixParser();
		FixBinder fb = new FixBinder(fp);
		fb.register(FixMessage1.class);
		fb.register(FixMessage2.class);
		fb.register(FixMessage3.class);
		fb.read(new MessageSource(new StringReader("8=FIX.4.1\00135=D\00110=10")));
		Assert.assertNotNull(fb.getCurrentMessageObject());
		Assert.assertTrue(fb.getCurrentMessageObject() instanceof FixMessage1);
		fb.read(new MessageSource(new StringReader("8=FIX.4.1\00135=8\00110=10")));
		Assert.assertNotNull(fb.getCurrentMessageObject());
		Assert.assertTrue(fb.getCurrentMessageObject() instanceof FixMessage2);
		
	}
	
	@Test(expected=FixBindingException.class)
	public void checkNoConstructorError() throws Exception
	{
		FixParser fp = new FixParser();
		FixBinder fb = new FixBinder(fp);
		fb.register(FixMessageNoConstructor.class);
		fb.read(new MessageSource(new StringReader("8=FIX.4.1\00135=FAIL\00110=10")));	
	}
	
}